//
//  JWTStoreCourseDetailViewController.m
//  JWT
//
//  Created by ndm on 17/2/10.
//  Copyright © 2017年 com.jwtsoft. All rights reserved.
//

#import "JWTStoreCourseDetailViewController.h"
#import "JWTStoreCourseView.h"
#import "JWTLoginViewController.h"

@interface JWTStoreCourseDetailViewController ()

@property (nonatomic,strong) UIScrollView *scrollView;

@property (nonatomic,strong) JWTStoreCourseView *storeCourseView;

@property (nonatomic,strong) UIButton *orderCourseBtn;

@property (nonatomic,strong) NSMutableArray *orderedArray;

@end

@implementation JWTStoreCourseDetailViewController

-(instancetype)init{
    self = [super init];
    if (self) {
        _orderedArray = [[NSMutableArray alloc] init];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initWithNavTitle:@"预约课程详情"];
    
    [self getJWTfindCourseMesDetail];
}

-(void) initScrollView{
    
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.left, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT-getNAVI_TOP_HEIGHT())];
        _scrollView.scrollEnabled = YES;
        //    _scrollView.backgroundColor = RGBA(150, 151, 152, 1);
        [self.view addSubview:_scrollView];
        
        _storeCourseView  = [[JWTStoreCourseView alloc] init];
        [_scrollView addSubview:_storeCourseView];
        
        [_storeCourseView.courseImageView sd_setImageWithURL:[NSURL URLWithString:_listCourse.picture] placeholderImage:[UIImage imageNamed:ImageViewDefault]];
        
        _storeCourseView.courseNameLabel.text = _listCourse.coursename;
        _storeCourseView.coachNameLabel.text = _listCourse.coachName;
        _storeCourseView.coursePriceLabel.text = [NSString stringWithFormat:@"¥%ld/课",_listCourse.price];
        _storeCourseView.courseTimeLabel.text = [NSString stringWithFormat:@"%@ ~ %@",[_listCourse.starttime substringWithRange:NSMakeRange(5, 11)],[_listCourse.endtime substringWithRange:NSMakeRange(5, 11)]];
        _storeCourseView.coursePersonLabel.text = [NSString stringWithFormat:@"%ld/%ld人",_listCourse.ordernum,_listCourse.totalnum];
        _storeCourseView.introLabel.text = @"简介";
        _storeCourseView.courseTitleLabel.text = _listCourse.introduce;
        _storeCourseView.courseTitleLabel.numberOfLines = 0;
        _storeCourseView.courseTitleLabel = [[JWTNewLabelFrame sharedInstance] newSizeFromLabel:_storeCourseView.courseTitleLabel];
        _storeCourseView.courseInfoView.frame =CGRectMake( _storeCourseView.courseInfoView.left,  _storeCourseView.courseInfoView.top, _storeCourseView.courseInfoView.width, _storeCourseView.courseTitleLabel.bottom+Cell_Margin);
        _storeCourseView.coachInfoView.frame = CGRectMake(_storeCourseView.coachInfoView.left, _storeCourseView.courseInfoView.bottom+Cell_Margin, _storeCourseView.coachInfoView.width, _storeCourseView.coachInfoView.height);
        _storeCourseView.storeInfoView.frame = CGRectMake(_storeCourseView.storeInfoView.left, _storeCourseView.coachInfoView.bottom+Cell_Margin, _storeCourseView.storeInfoView.width, _storeCourseView.storeInfoView.height);
        _storeCourseView.orderInfoView.frame = CGRectMake(_storeCourseView.orderInfoView.left, _storeCourseView.storeInfoView.bottom+Cell_Margin, _storeCourseView.orderInfoView.width, _storeCourseView.orderInfoView.height);
        _storeCourseView.timeInfoView.frame = CGRectMake(_storeCourseView.timeInfoView.left, _storeCourseView.orderInfoView.bottom+Cell_Margin, _storeCourseView.timeInfoView.width, _storeCourseView.timeInfoView.height);
        _storeCourseView.frame = CGRectMake(_storeCourseView.left, _storeCourseView.top, _storeCourseView.width, _storeCourseView.timeInfoView.bottom+Cell_Margin);
        //
        _storeCourseView.coachLabel.text = @"教练";
        [_storeCourseView.coachImageView sd_setImageWithURL:[NSURL URLWithString:_listCourse.coachName] placeholderImage:[UIImage imageNamed:ImageViewDefault]];
        _storeCourseView.coachSignLabel.text = _listCourse.coachSignature;
        _storeCourseView.coachSignLabel.numberOfLines = 0;
        _storeCourseView.coachSignLabel = [[JWTNewLabelFrame sharedInstance] newSizeFromLabel:_storeCourseView.coachSignLabel];
        
        //
        _storeCourseView.storeLabel.text = @"场地";
        [_storeCourseView.storeImageView sd_setImageWithURL:[NSURL URLWithString:_listCourse.storePicture] placeholderImage:[UIImage imageNamed:ImageViewDefault]];
        _storeCourseView.storeNameLabel.text = _listCourse.storeNickname;
        _storeCourseView.storeArressLabel.text = _listCourse.storeAddress;
        _storeCourseView.storeArressLabel.numberOfLines = 0;
        _storeCourseView.storeArressLabel = [[JWTNewLabelFrame sharedInstance] newSizeFromLabel:_storeCourseView.storeArressLabel];
        
        //
        _storeCourseView.orderCourseLabel.text = [NSString stringWithFormat:@"已报名成员(%ld/%ld人)",_listCourse.ordernum,_listCourse.totalnum];
        
        
        
        //
        _storeCourseView.courTimeTitleLabel.text = @"课程时间";
        _storeCourseView.courTimeLabel.text = _listCourse.starttime;
        
        _scrollView.contentSize = CGSizeMake(_storeCourseView.width, _storeCourseView.height);
        
        //预约按钮
        CGFloat btn_width = 50;
        _orderCourseBtn = [[UIButton alloc] initWithFrame:CGRectMake(UISCREEN_WIDTH-btn_width-3*Cell_Margin, UISCREEN_HEIGHT-2*getNAVI_TOP_HEIGHT(),btn_width, btn_width)];
        [_orderCourseBtn setImage:[UIImage imageNamed:@"detail_book_add"] forState:UIControlStateNormal];
        [_orderCourseBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateDisabled];
        [self getJWTCourseOrderMember];
        //    _orderCourseBtn.hidden = YES;
        [self.view addSubview:_orderCourseBtn];
        
        
        
        WS(weakSelf);
        [_orderCourseBtn addButtonBlockControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
            [weakSelf judgeUserIsLogin];
        }];
        
        JWTDateString *dateString = [JWTDateString sharedInstance];
        NSString *cur_date_string = [dateString stringFromDate:[NSDate date]];
        if ([dateString compareSmallerTwoDate:cur_date_string Other:_listCourse.starttime] == YES) {
            
        }
    }
}

/**判断用户是否登录*/
-(void) judgeUserIsLogin{
    if (APPDELEGATE.isLogin == YES) {
        if ([_listCourse.isordered integerValue] == 0) {
            if ([APPDELEGATE.userInfo.cardname isEqualToString:@""]||[APPDELEGATE.userInfo.cardname isEqualToString:@"非会员"]) {
                [HUD showToast:@"非会员不能预约"];
            }else{
                [self getJWTOrderCourse];
            }
            
        }else{
            
        }
    }else{
        //通知注册 用户登录成功自动回调预约或者取消接口
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(request_orderCourseAction:) name:@"request_orderCourseAction" object:nil];
        
        JWTLoginViewController *vc  = [[JWTLoginViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(void) request_orderCourseAction:(NSNotification *) notice{
    [_scrollView removeFromSuperview];
    _scrollView = nil;
    [self getJWTfindCourseMesDetail];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/**
 预约课程
 */
-(void) getJWTOrderCourse{
    
    NSString *urlString = [[JWTAPPHttpsUrl sharedInstance] getJWTOrderCourse];
    [HUD showUIBlockingIndicator];
    NSDictionary *dict = @{@"tokenid":APPDELEGATE.userInfo.tokenid,@"courseid":_listCourse.courseid};
    [AFNHttpsRequest requestWithUrl:urlString WithParam:dict WithGetDataDidFinishBlock:^(id model, NSURLSessionDataTask *task, NSError *error) {
        [HUD hideUIBlockingIndicator];
//        if (model) {
//            if ([[model objectForKey:@"code"]integerValue] == 200) {
//                NSLog(@"%@",model);
//                [HUD showToast:[model objectForKey:@"message"]];
//            }
//        }
        [HUD showToast:[model objectForKey:@"message"]];
    }];
}

/**
 取消课程
 */
-(void) getJWTUnOrderCourse{
    
    NSString *urlString = [[JWTAPPHttpsUrl sharedInstance] getJWTUnOrderCourse];
    [HUD showUIBlockingIndicator];
    NSDictionary *dict = @{@"tokenid":APPDELEGATE.userInfo.tokenid,@"courseid":_listCourse.courseid};
    [AFNHttpsRequest requestWithUrl:urlString WithParam:dict WithGetDataDidFinishBlock:^(id model, NSURLSessionDataTask *task, NSError *error) {
        [HUD hideUIBlockingIndicator];
        if (model) {
            if ([[model objectForKey:@"code"]integerValue] == 200) {
                NSLog(@"%@",model);
                
            }
        }
    }];
}

/**
 获取数据详情接口
 */
-(void) getJWTfindCourseMesDetail{
   
    NSString *tel ;
    if (APPDELEGATE.isLogin == YES) {
        tel = APPDELEGATE.userInfo.tel;
    }else{
        tel = @"";
    }
    NSString *urlString = [[JWTAPPHttpsUrl sharedInstance] getJWTfindCourseMesDetailWithCourseID:_courseID andTel:tel];
    [HUD showUIBlockingIndicator];
    [AFNHttpsRequest requestWithUrl:urlString WithParam:nil WithGetDataDidFinishBlock:^(id model, NSURLSessionDataTask *task, NSError *error) {
        [HUD hideUIBlockingIndicator];
        if (model) {
            if ([[model objectForKey:@"code"]integerValue] == 200) {
                NSLog(@"%@",model);
                _listCourse = [JWTListCourse mj_objectWithKeyValues:[model objectForKey:@"data"]];
                [self initScrollView];
            }
        }
    }];
}


/**
 获取成员数量
 */
-(void) getJWTCourseOrderMember{
    NSString *urlString = [[JWTAPPHttpsUrl sharedInstance] getJWTCourseOrderMemberWithCourseID:_listCourse.courseid];
    [HUD showUIBlockingIndicator];
    [AFNHttpsRequest requestWithUrl:urlString WithParam:nil WithGetDataDidFinishBlock:^(id model, NSURLSessionDataTask *task, NSError *error) {
        [HUD hideUIBlockingIndicator];
        if (model) {
            if ([[model objectForKey:@"code"]integerValue] == 200) {
                NSLog(@"%@",model);
                _orderedArray = [NSMutableArray arrayWithArray:[[model objectForKey:@"data"] objectForKey:@"list"]];
                [_storeCourseView createOrderedPersonScrollView:_storeCourseView.orderIconScrollView orderedArray:_orderedArray];
                
                _storeCourseView.orderIconScrollView.contentSize = CGSizeMake(Cell_Margin*_orderedArray.count+50*_orderedArray.count+Cell_Margin, _storeCourseView.orderIconScrollView.height);
            }
        }
    }];
}


@end
